package com.dreamdance.queerball;

import android.content.Context;

import android.content.res.Resources;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.KeyEvent;
import android.view.MotionEvent;
import net.ds0101.framework.android.neox.action.NXAction;
import net.ds0101.framework.android.neox.action.NXFadeIn;
import net.ds0101.framework.android.neox.main.NXDirector;
import net.ds0101.framework.android.neox.main.NXScene;
import net.ds0101.framework.android.neox.util.NXResourceConverter;

import net.ds0101.framework.android.neox.widget.NXBitmapSheet;

public class QueerBallGameScene extends NXScene {
    private ChessboardData	mDataModule;
    private ChessboardUI	mChessboard;
    private GameOverUI mGameOver;
    private Paint		mPaint;
    private final String mNextSet;
	private NXAction mDisplay;

	public QueerBallGameScene(Context context) {
		super(context);
        Resources res = context.getResources();
        mNextSet = res.getString(R.string.next_set);
		NXBitmapSheet backGroud = new NXBitmapSheet(NXResourceConverter.getBitmapFromResource(this, R.drawable.chessboard));
        backGroud.position(NXDirector.getScreenWidth() / 2, NXDirector.getScreenHeight() / 2);
        mDataModule = new ChessboardData();
        mChessboard = new ChessboardUI(this, backGroud, mDataModule);
        mGameOver = new GameOverUI(this);
        mGameOver.visible(false);
        mGameOver.opacity(0);
        addChild(mGameOver, 2);
        mDataModule.registerDataChangedListener(mChessboard);
        mChessboard.position(NXDirector.getScreenWidth() / 2, NXDirector.getScreenHeight() / 2);
        addChild(mChessboard, 1);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mDisplay = NXFadeIn.action(300);
	}
	
	@Override
	public void onEnter() {
		gameStart();
	}

	@Override
	public void onScreenSizeChanged(int arg0, int arg1) {

	}

	@Override
	public void update(int arg0) {
        /*mDataModule.newBallsGenerate();
        mDataModule.putNewBallsToGrid();
        mDataModule.findEliminatable();
        mDataModule.clearMarkedGrid();*/
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean ret = super.onTouchEvent(event);
		if (mChessboard.visible() 
				&& !mChessboard.areaContains(event.getX(), event.getY()) 
				&& mChessboard.selectedValid()) {
			mChessboard.resetSelected(); // if touch the area out of chessboard then reset the selected.
		}
		((NXDirector)getContext()).popScene();
		return ret;
	}
	
	public void gameStart() {
		//mGameOver.visible(false);
		if (mGameOver.visible()) {
			mGameOver.stopAction(mDisplay);
			mGameOver.close();
		}
		mChessboard.onGameStart();
		mChessboard.setEnable(true);
        mDataModule.reset();
        mDataModule.newBallsGenerate();
        mDataModule.putNewBallsToGrid();
        mDataModule.findEliminatable();
        mDataModule.clearMarkedGrid();
	}
	
	public void gameOver() {
		mChessboard.setEnable(false);
		mGameOver.visible(true);
		mGameOver.runAction(mDisplay);
	}
}
